package day0401;

import java.util.*;
import java.util.stream.Collectors;

public class ConnectArray8 {
    /**
     * 8.【数组合并】问题
     * <p>
     * 输入
     * 3
     * 2
     * 2,5,6,7,9,5,7
     * 1,7,4,3,4
     * 输出
     * 2,5,6,1,7,4,7,9,5,3,4,7
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int splitValue = Integer.parseInt(sc.nextLine());
        int n = Integer.parseInt(sc.nextLine());
        String[] strings = new String[n];
        for (int i = 0; i < n; i++) {
            strings[i] = sc.nextLine();
        }
        List<LinkedList<Integer>> lists = new ArrayList<>(n);
        for (String string : strings) {
            LinkedList<Integer> collect = Arrays.stream(string.split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toCollection(LinkedList::new));
            lists.add(collect);
        }
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (lists.size() > 0) {
            LinkedList<Integer> list = lists.get(index);
            for (int i = 0; i < splitValue; i++) {
                if (list.isEmpty()) {
                    lists.remove(list);
                    index--;
                    break;
                }
                sb.append(list.removeFirst()).append(",");
            }
            index++;
            if (index >= lists.size()) {
                index = 0;
            }

        }
        System.out.println(sb.substring(0,sb.length()-1));
    }
}
